package csu.web.mypetstore.persistence;
import java.sql.*;
/*
statement执行不要参数的sql
preparedstatement执行要参数的sql
 */
public class DBUtil {
    //导入驱动和连接mysql用到的硬编码
    private static final String DRIVER="com.mysql.jdbc.Driver";
    private static final String URL="jdbc:mysql://127.0.0.1:3306/mypetstore?useSSL=false";
    private static final String USERNAME="root";
    private static final String PASSWORD="khy123...";

    //封装数据库连接-导入驱动+连接数据库
    public static Connection getConnection(){
        Connection connection=null;
        try{
            Class.forName(DRIVER);
            connection= DriverManager.getConnection(URL,USERNAME,PASSWORD);
        }catch(Exception e){
            e.printStackTrace();
        }
        return connection;
    }

    //封装关闭连接
    public static void closeConnection(Connection connection){
        if(connection!=null){
            try{
                connection.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    public static void closeStatement(Statement statement){
        if(statement!=null){
            try{
                statement.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    public static void closePreparedStatement(PreparedStatement preparedStatement){
        if(preparedStatement!=null){
            try{
                preparedStatement.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
    //结果集--Query语句执行结果用结果集接收
    public static void closeResultSet(ResultSet resultSet){
        if(resultSet!=null){
            try{
                resultSet.close();
            }catch (Exception e){
                e.printStackTrace();

            }
        }
    }

    public static void main(String[] args) {
        System.out.println(getConnection());
    }
}
